package dao;

import com.mysql.jdbc.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//对博文进行增，删，改，查
public class BlogDao {

    //新建博文
    public void insert(Blog blog)
    {
        Connection connection=null;
        PreparedStatement statement=null;
        String sql="insert into blog values(null,?,?,?,now());";

        try {
            connection=DBUtil.getConnection();
            statement=connection.prepareStatement(sql);
            //statement.setInt(1,blog.getBlogId());
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setInt(3,blog.getUserId());

            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }

    //获取博文
    public List<Blog> getBlogs(int id){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        List<Blog> t1=new ArrayList<>();
        try{
            connection=DBUtil.getConnection();

            String sql="select * from blog where userId=? order by postTime desc;";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            resultSet=statement.executeQuery();

            while(resultSet.next())
            {
                Blog blog =new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                t1.add(blog);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return t1;
    }

    //获取当前登陆者的博客数目
    public int getBlognum(int userId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try{
            connection = DBUtil.getConnection();
            String sql = "select max(blogId) totalCount from blog where userId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            resultSet= statement.executeQuery();
            if(resultSet.next())
            {
                return resultSet.getInt("totalCount");
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }

    }

    //获取第几篇博文
    public Blog getBlog(int blogId,int userId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();

            String sql = "select * from blog where blogId=? and userId=?";

            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            statement.setInt(2,userId);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
    //删除指定博文
    public void delete(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "delete from blog where blogId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

}
